package mapEditor.view.assetEdit
{
	import asset.mapEditorAssetItemAsset;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.AssetVO;
	
	import utils.ApplicationDomainUtil;
	import utils.DisplayUtil;
	
	public class AssetItemView extends mapEditorAssetItemAsset
	{
		private var _assetObject:DisplayObject;
		/**
		 *资源对象类型:1=BitmapData;2=MovieClip
		 */
		private var _assetType:int;
		/**
		 *资源类型：0=未入库新资源;1=已入库资源
		 */
		private var _type:int;
		private var _assetVO:AssetVO;
		/**
		 *是否选中 
		 */		
		private var _isSelected:Boolean;
		/**
		 *是否编辑中 
		 */		
		private var _isEdit:Boolean;
		
		public function AssetItemView(type:int, assetVO:AssetVO)
		{
			_type=type;
			_assetVO=assetVO;
			
			gotoAndStop(1);
			var obj:*=ApplicationDomainUtil.getDefinitionObject(_assetVO.className);
			if(obj is BitmapData)
			{
				_assetObject=new Bitmap(obj);
			}
			else if(obj is MovieClip)
			{
				_assetObject=obj as DisplayObject;
			}
			if(_assetObject.width>60)_assetObject.width=60;
			if(_assetObject.height>60)_assetObject.height=60;
			_assetObject.x=(60-_assetObject.width)/2;
			_assetObject.y=(60-_assetObject.height)/2;
			posShow.addChild(_assetObject);
			
			doubleClickEnabled=true;
			mouseChildren=false;
			addEventListener(MouseEvent.MOUSE_OVER, __onMouseEvent);
			addEventListener(MouseEvent.MOUSE_OUT, __onMouseEvent);
			addEventListener(MouseEvent.DOUBLE_CLICK, __onMouseEvent);
			addEventListener(MouseEvent.CLICK, __onMouseEvent);
			_assetVO.addEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
			
			setInfo();
		}
		
		private function setInfo():void
		{
			txtName.text=_assetVO.name;
			txtID.text=_assetVO.id.toString();
		}
		
		private function __onPropertyChange(evt:MapEditorEvent):void
		{
			setInfo();
		}
		
		private function __onMouseEvent(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.MOUSE_OVER:
					if(!_isSelected)gotoAndStop(2);
					break;
				case MouseEvent.MOUSE_OUT:
					if(!_isSelected)gotoAndStop(1);
					break;
				case MouseEvent.DOUBLE_CLICK:
					isEdit=true;
					break;
				case MouseEvent.CLICK:
					isSelected=true;
					break;
			}
		}
		
		public function dispose():void
		{
			removeEventListener(MouseEvent.MOUSE_OVER, __onMouseEvent);
			removeEventListener(MouseEvent.MOUSE_OUT, __onMouseEvent);
			removeEventListener(MouseEvent.DOUBLE_CLICK, __onMouseEvent);
			removeEventListener(MouseEvent.CLICK, __onMouseEvent);
			if(_assetVO)
			{
				_assetVO.removeEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
				_assetVO.dispose();
				_assetVO=null;
			}
			DisplayUtil.dispose(_assetObject);
			_assetObject=null;
			if(parent)parent.removeChild(this);
		}

		/**
		 *是否选中
		 */
		public function get isSelected():Boolean
		{
			return _isSelected;
		}

		/**
		 * @private
		 */
		public function set isSelected(value:Boolean):void
		{
			if(_isSelected == value)return;
			_isSelected = value;
			gotoAndStop(_isSelected ? 2 : 1);
			if(_isSelected)dispatchEvent(new MapEditorEvent(MapEditorEvent.SELECTED));
		}

		public function get assetVO():AssetVO
		{
			return _assetVO;
		}

		/**
		 *是否编辑中
		 */
		public function get isEdit():Boolean
		{
			return _isEdit;
		}

		/**
		 * @private
		 */
		public function set isEdit(value:Boolean):void
		{
			_isEdit = value;
			if(_isEdit)dispatchEvent(new MapEditorEvent(MapEditorEvent.EDITING));
		}
	}
}